package com.bilibili.dubbo.api;

import com.bilibili.dubbo.api.mongo.VisitorsApi;
import com.bilibili.model.mongo.Visitors;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

/**
 * @author HuangRongFu
 * 2021/11/25 星期四9:20 下午
 */

@DubboService
public class VisitorsApiImpl implements VisitorsApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void save(Visitors visitors) {

        Query query = new Query(Criteria.where("userId").is(visitors.getUserId())
                .and("visitorUserId").is(visitors.getVisitorUserId()).and("visitDate").is(visitors.getVisitDate()));

        if (!mongoTemplate.exists(query, Visitors.class)){

            mongoTemplate.insert(visitors);
        }
    }
}
